package com.mt.channel.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.entity.ChannelAccountEntity;
import com.mt.channel.entity.ChannelOrderEntity;
import com.mt.channel.mapper.ChannelAccountEntityMapper;
import com.mt.channel.mapper.ChannelAccountLogEntityMapper;
import com.mt.channel.mapper.ChannelOrderEntityMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;

@Component
public class ChannelOrderServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelOrderServiceModel.class);

    @Autowired
    private ChannelOrderEntityMapper channelOrderEntityMapper;
    @Autowired
    private ChannelAccountEntityMapper channelAccountEntityMapper;
    @Autowired
    private ChannelAccountLogEntityMapper channelAccountLogEntityMapper;

    public ServiceResult<List<ChannelOrderEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<List<ChannelOrderEntity>> result = new ServiceResult<List<ChannelOrderEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            result.setResult(this.channelOrderEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ChannelOrderEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<ChannelOrderEntity> result = new ServiceResult<ChannelOrderEntity>();
        try {
            result.setResult(this.channelOrderEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ChannelOrderEntity channelOrderEntity) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelOrderEntityMapper.insert(channelOrderEntity);
            if (save > 0) {
                id = channelOrderEntity.getChannelOrderId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> selectCount(Map<String, Object> params) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result.setResult(this.channelOrderEntityMapper.selectCount(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<Integer> insertRelate(ChannelOrderEntity channelOrderEntity,
        ChannelAccountEntity channelAccountEntity) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelOrderEntityMapper.insert(channelOrderEntity);
            if (save > 0) {
                id = channelOrderEntity.getChannelOrderId();

                if (channelAccountEntity != null) {
                    channelAccountEntityMapper.updateByPrimaryKeySelective(channelAccountEntity);
                    if (channelAccountEntity.getChannelAccountLogList() != null) {
                        channelAccountLogEntityMapper.insertList(channelAccountEntity.getChannelAccountLogList());
                    }
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][insert]：query insert occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ChannelOrderEntity channelOrderEntity) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelOrderEntityMapper.updateByPrimaryKeySelective(channelOrderEntity);
            if (save > 0) {
                id = channelOrderEntity.getChannelOrderId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ChannelOrderEntity channelOrderEntity) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelOrderEntityMapper.updateByPrimaryKeySelective(channelOrderEntity);
            if (save > 0) {
                id = channelOrderEntity.getChannelOrderId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer channelOrderId) {
        Assert.notNull(this.channelOrderEntityMapper, "Property 'channelOrderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelOrderEntityMapper.deleteByPrimaryKey(channelOrderId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelOrderServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
